package com.fiberhome.platform.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fiberhome.platform.common.PageResult;
import com.fiberhome.platform.entity.DesignNeedOut;
import com.fiberhome.platform.entity.TestCaseOut;
import com.fiberhome.platform.pojo.DesignNeed;
import com.fiberhome.platform.pojo.NeedCategory;
import com.fiberhome.platform.pojo.TestCase;
import com.fiberhome.platform.pojo.TestCategory;
import com.fiberhome.platform.service.NeedCatService;
import com.fiberhome.platform.service.NeedService;
import com.fiberhome.platform.service.TestCatService;
import com.fiberhome.platform.service.TestService;


@Controller
public class ContentController {
	@Autowired
	private NeedService needService;
	
	@Autowired
	private NeedCatService needCatService;
	@Autowired
	private TestCatService testCatService;
	
	@Autowired
	private TestService testService;

//	@RequestMapping(value = "/need/query/list")
//	@ResponseBody
//	public EUDataGridResult getNeedList(Integer categoryId, int page, int rows) {
//		return needService.getNeedList(categoryId, page, rows);
//	}

	@RequestMapping(value="/need/querybyno")
	public String getNeedByNo(NeedCategory needIn, Model model) {
		if ((needIn.getDesignNo()==null || needIn.getDesignNo()=="")) {
			if (needIn.getIsParent()==1) {
				return "blank";
			}
			NeedCategory need = new NeedCategory();
			need.setId(needIn.getId());
			need.setName(needIn.getName());
			need.setIsPon(needIn.getIsPon());
			model.addAttribute("need",need);
			return "need-add";
		}
		
		DesignNeedOut need = needService.getNeedByNo(needIn.getDesignNo());
		if (need==null) {
			return "need-add";
		}
		model.addAttribute("need", need);
		return "need-detail";
	}
	
	@RequestMapping(value="/test/querybyno")
	public String getTestByNo(TestCategory testIn, Model model) {
		if (testIn.getCaseNo()==null || testIn.getCaseNo()=="") {
			if (testIn.getIsParent()==1) {
				return "blank";
			}
			TestCategory test = new TestCategory();
			test.setId(testIn.getId());
			test.setName(testIn.getName());
			model.addAttribute("test",test);
			return "test-add";
		}
		TestCaseOut test = testService.getTestByNo(testIn.getCaseNo());
		if (test==null) {
			return "test-add";
		}
		model.addAttribute("test", test);
		return "test-detail";
	}
	
	@RequestMapping(value="/need/isdup", method=RequestMethod.GET)
	@ResponseBody
	public PageResult isDupNeed(String designNo) {
		return needService.isDup(designNo);
	}
	
	@RequestMapping(value = "/need/save", method = RequestMethod.POST)
	@ResponseBody
	public PageResult saveNeed(DesignNeed need) {
		return needService.saveNeed(need);
	}
	
	@RequestMapping(value="/need/editpage", method=RequestMethod.GET)
	public String showEditNeedPage(DesignNeed need, Model model) {
		model.addAttribute("need", needService.getNeedByNo(need.getDesignNo()));
		return "need-edit";
	}
	
	@RequestMapping(value="/need/edit", method=RequestMethod.POST)
	@ResponseBody
	public PageResult editNeed(DesignNeed need) {
		return needService.editNeed(need);
	}
	
	@RequestMapping(value="/need/delete")
	@ResponseBody
	public PageResult deleteNeed(Integer id) {
		long categoryId = (long) needService.deleteNeed(id).getData();
		return PageResult.ok(categoryId);
	}
	
	@RequestMapping(value="/test/isdup", method=RequestMethod.GET)
	@ResponseBody
	public PageResult isDupTest(String caseNo) {
		return testService.isDup(caseNo);
	}
	
	@RequestMapping(value = "/test/save", method = RequestMethod.POST)
	@ResponseBody
	public PageResult saveTest(TestCase test) {
		return testService.saveTest(test);
	}
	
	@RequestMapping(value="/test/editpage", method=RequestMethod.GET)
	public String showEditTestPage(TestCase test, Model model) {
		TestCaseOut test2 = testService.getTestByNo(test.getCaseNo());
		System.out.println(test2.getFma());
		model.addAttribute("test", test2);
		return "test-edit";
	}
	
	@RequestMapping(value="/test/edit", method=RequestMethod.POST)
	@ResponseBody
	public PageResult editTest(TestCase test) {
		return testService.editTest(test);
	}
	
	@RequestMapping(value="/test/delete")
	@ResponseBody
	public PageResult deleteTest(Integer ids) {
		long categoryId = (long) testService.deleteTest(ids).getData();
		return PageResult.ok(categoryId);
	}
}
